package com.idak.jaxb;

import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXB;

public class QueryLoaderImpl implements QueryLoader {
	
	private final Map<String, Map<String, String>> queriesGroup = new LinkedHashMap<String, Map<String,String>>();

	public QueryLoaderImpl(InputStream xmlStream) {
		Queries queries = JAXB.unmarshal(xmlStream, Queries.class);
		List<QueryGroup> groups = queries.getGroups();
		for (QueryGroup group : groups) {
			String groupName = group.getName();
			Map<String, String> queryGroupe = new LinkedHashMap<String, String>();
			queriesGroup.put(groupName, queryGroupe);
			for(Query query : group.getQueries()){
				queryGroupe.put(query.getName(), query.getQuery().trim());
			}
		}
	}

	@Override
	public Map<String, String> getQueries(String groupName) {
		return queriesGroup.get(groupName);
	}

	@Override
	public String getQuery(String groupName, String queryName) {
		return queriesGroup.get(groupName).get(queryName);
	}

}
